package com.example.mentalhealth.Controller.AdminController;

import com.example.mentalhealth.Service.*;
import com.example.mentalhealth.entities.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

//答卷管理
@RestController
@RequestMapping("/api")
public class AnswerManagement {

    @Autowired
    private ExamRecordService examRecordService;

    @Autowired
    private ExamRecordQuestionService examRecordQuestionService;

    @Autowired
    private UserService userService;

    @Autowired
    private ExamPaperService examPaperService;

    @Autowired
    private QuestionService questionService;


    //获取答卷
    @GetMapping("/GetExamRecord")
    public ResultData getExamRecord(
            @RequestParam(required = false) String paperTitle,//试卷标题
            @RequestParam(required = false) String studentName,//学生姓名
            @RequestParam(required = false) String scoreRange,//分数范围
            @RequestParam(required = false) List<String> dateRange,//时间范围
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ){
        //1.通过用户名查询用户id
        Long userId = userService.getIdByUserName(studentName);
        //2.通过试卷名称查询试卷id
        Long paperId = examPaperService.getIdByTitle(paperTitle);
        //3.创建答卷对象
        ExamRecord examRecord = new ExamRecord();
        examRecord.setUserId(userId);//设置用户id
        examRecord.setPaperId(paperId);//设置试卷id
        LocalDateTime startTime = null;
        LocalDateTime endTime = null;
        int startScore = 0;
        int endScore = 100;
        //处理分数范围
        if (!"all".equals(scoreRange)) {
            String[] parts = scoreRange.split("-");
            startScore = Integer.parseInt(parts[0]);
            endScore = Integer.parseInt(parts[1]);
        }
        //处理时间范围
        if (dateRange != null && dateRange.size() == 2) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            startTime = LocalDateTime.parse(dateRange.get(0), formatter);
            endTime = LocalDateTime.parse(dateRange.get(1), formatter)
                    .withHour(23).withMinute(59).withSecond(59);
        }
        //4.查询答卷
        PageInfo<ExamRecordResultData> examRecordPageInfo = examRecordService.selectByQuery(examRecord,startTime, endTime, startScore, endScore,page, size);

        System.out.println(examRecordPageInfo);
        return new ResultData(200, "success", examRecordPageInfo);
    }

    //返回需要批改的答卷
    @GetMapping("/GetExamRecordQuestion")
    public ResultData getExamRecordQuestion(){
        //返回答卷问题
        List<ExamRecordQuestion> examRecordQuestions = examRecordQuestionService.queryExamRecordQuestion();
        for (ExamRecordQuestion examRecordQuestion : examRecordQuestions) {
            Question question = questionService.queryQuestionsByID(Math.toIntExact(examRecordQuestion.getQuestionId()));
            //获取问题类型
            examRecordQuestion.setType(question.getType());
            //获取问题内容
            examRecordQuestion.setContent(question.getContent());
            //获取问题答案
            examRecordQuestion.setCorrectAnswer(question.getAnswer());
            //获取问题满分
            examRecordQuestion.setFullScore(question.getScore());
        }
        return new ResultData(200, "success", examRecordQuestions);
    }

    //更新分数
    @PutMapping("/UpdateExamRecordQuestion")
    public ResultData updateScore(@RequestBody  ExamRecordQuestion examRecordQuestion){
        Long id = examRecordQuestion.getId();//获取id
        BigDecimal score = examRecordQuestion.getScore();//获取分数
        //获取试题原本分数
        BigDecimal originalScore = questionService.queryQuestionsByID(Math.toIntExact(examRecordQuestion.getQuestionId())).getScore();
        if(score.compareTo(originalScore) == 0){
            examRecordQuestionService.updateScoreById(id, score,1);//更新试题分数
        }else if(score.compareTo(originalScore) < 0){
            examRecordQuestionService.updateScoreById(id, score,0);//更新试题分数
        }else {
            examRecordQuestionService.updateScoreById(id, score,2);//更新试题分数
        }

        //更新总分
        Long record_id = examRecordQuestion.getRecordId();//获取记录id
        BigDecimal score1 = new BigDecimal("0");//总分
        for (ExamRecordQuestion examRecordQuestion1 : examRecordQuestionService.queryByExamRecordId(record_id)) {
            score1 = score1.add(examRecordQuestion1.getScore());
        }
        examRecordService.updateScoreById(record_id,score1);//更新总分 正确

        return new ResultData(200, "success");
    }









}
